package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.areas.TbAreas;
import com.dongdongshop.pojo.cities.TbCities;
import com.dongdongshop.pojo.provinces.TbProvinces;
import com.dongdongshop.service.areas.AreasService;
import com.dongdongshop.service.cities.CitiesService;
import com.dongdongshop.service.provinces.ProvincesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("provincial")
public class ProvincialController {
    @Reference
    private ProvincesService provincesService;
    @Reference
    private CitiesService citiesService;
    @Reference
    private AreasService areasService;
    private Logger logger = LoggerFactory.getLogger(ProvincialController.class);
    //获取省级
    @RequestMapping("listProvincial")
    @ResponseBody
    public Result listProvinces(){
        try {
            List<TbProvinces> provincesList = provincesService.listProvinces();
            return Result.ok().setData(provincesList);

        }catch (Exception e){
            logger.error("查询省级失败",e);
            return Result.fail();
        }
    }

    //获取市
    @RequestMapping("listCitiesByProvinceid")
    @ResponseBody
    public Result listCitiesByProvinceid(String provinceid){
        try {
            List<TbCities> citiesList = citiesService.listCitiesByProvinceid(provinceid);
            return Result.ok().setData(citiesList);

        }catch (Exception e){
            logger.error("查询市级失败",e);
            return Result.fail();
        }
    }
    //获取区
    @RequestMapping("listAreasByCityid")
    @ResponseBody
    public Result listAreasByCityid(String cityid){
        try {
            List<TbAreas> areasList = areasService.listAreasByCityid(cityid);
            return Result.ok().setData(areasList);

        }catch (Exception e){
            logger.error("查询区级失败",e);
            return Result.fail();
        }
    }
}
